home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / server / scripts / scene.cs < prev    next >
Text File  |  2006-09-21  |  14KB  |  542 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------------------
  7. //  Functions that implement game-play
  8. //-----------------------------------------------------------------------------
  9.  
  10. package DemoSceneGame {
  11.  
  12. //-----------------------------------------------------------------------------
  13.  
  14. function GameConnection::onClientEnterGame(%this)
  15. {
  16.    commandToClient(%this,'SyncClock', $Sim::Time - $Game::StartTime);
  17.    commandToClient(%this,'SetGameGUI',"SceneGui");
  18.  
  19.    // Create a new camera object.
  20.    %this.camera = new Camera() {
  21.       dataBlock = Observer;
  22.    };
  23.    MissionCleanup.add( %this.camera );
  24.    %this.camera.scopeToClient(%this);
  25.    
  26.    // Create path camera
  27.    %this.player = new PathCamera() {
  28.       dataBlock = LoopingCam;
  29.       position = Scene::getStartPos();
  30.    };
  31.    MissionCleanup.add( %this.player );
  32.    %this.player.scopeToClient(%this);
  33.  
  34.    // Start up the room gui system
  35.    $Server::Client = %this;
  36.    %this.setControlObject(%this.player);
  37.    SceneGui.setSceneNumber(0);
  38.    
  39.    // Force detail settings
  40.    $Pref::TS::detailAdjust = 1;
  41.    $Pref::TS::screenError = 10;
  42.    
  43.    // Start NPC orcs
  44.    newNPCOrc("MissionGroup/Paths/OrcNPC1",0.75);   
  45.    newNPCOrc("MissionGroup/Paths/OrcNPC2",0.75);   
  46.    newNPCOrc("MissionGroup/Paths/OrcNPC3",0.40);   
  47. }
  48.  
  49. //-----------------------------------------------------------------------------
  50.  
  51. function GameConnection::createPlayer(%this, %spawnPoint)
  52. {
  53. }
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57.  
  58. function serverCmdSuicide(%client)
  59. {
  60.    if (isObject(%client.player)) {
  61.       %client.player.delete();
  62.       %client.player = 0;
  63.    }
  64.    %client.spawnPlayer();
  65. }
  66.  
  67. };
  68.  
  69.  
  70. //-----------------------------------------------------------------------------
  71. // Scene Class
  72. //-----------------------------------------------------------------------------
  73.  
  74. $Server::CurrentScene = 0;
  75.  
  76. // Scene activation is driven from the DemoGui and related scripts.
  77. // (see demo/client/scripts/demoGui.cs)
  78. function Scene::activateScene(%number)
  79. {
  80.    if (isObject($Server::CurrentScene))
  81.       $Server::CurrentScene.close();
  82.  
  83.    $Server::CurrentScene = "MissionGroup/Scenes".getObject(%number);
  84.    echo("Activating Scene #"@%number);
  85.    $Server::CurrentScene.open();
  86.    return $Server::CurrentScene;
  87. }
  88.  
  89. function Scene::open(%this)
  90. {
  91.    echo("Scene " @ %this.getName() @ " open");
  92.    %client = $Server::Client;
  93.  
  94.    // Push any scene specific controls
  95.    if (isObject(%this.gui))
  96.       Canvas.pushDialog(%this.gui);
  97.       
  98.    // Set the path to follow, the camera will start at
  99.    // the first node in the path.
  100.    %path = %this.getId() @ "/path";
  101.    if (isObject(%path)) {
  102.       %client.player.reset(0);
  103.       %client.player.followPath(%path);
  104.    }
  105.    else {
  106.       // Check for a static camera
  107.       %start = %this.getId() @ "/start";
  108.       if (isObject(%start)) {
  109.          echo("Static Camera");
  110.          %client.player.reset(0);
  111.          %client.player.pushNode(%start);
  112.          %client.player.popFront();
  113.       }
  114.    }
  115. }
  116.  
  117. function Scene::getStartPos()
  118. {
  119.    %scene = "MissionGroup/Scenes".getObject(0);
  120.    %start = %scene.getId() @ "/start";
  121.    if (isObject(%start))
  122.       return %start.getTransform();
  123.    return "0 0 100";
  124. }
  125.  
  126. function Scene::close(%this)
  127. {
  128.    %client = $Server::Client;
  129.    
  130.    // Pop any scene specific controls
  131.    if (isObject(%this.gui))
  132.       Canvas.popDialog(%this.gui);
  133.    
  134.    echo("Scene " @ %this.getName() @ " closed");
  135. }
  136.  
  137.  
  138. //-----------------------------------------------------------------------------
  139. // Orc Animation
  140. //-----------------------------------------------------------------------------
  141.  
  142. function newNPCOrc(%path,%speed)
  143. {
  144.    %orc = AIPlayer::spawnOnPath("NPC",%path);
  145.    %orc.setMoveSpeed(%speed);
  146.    %orc.mountImage(CrossbowImage,0);
  147.    %orc.pushTask("followPath(\""@%path@"\",-1)");
  148. }
  149.  
  150.  
  151. function newAnimationOrc()
  152. {
  153.    if (isObject($AnimationOrc))
  154.       $AnimationOrc.clearTasks();
  155.    else {
  156.       $AnimationOrc = AIPlayer::spawnOnPath("Tarkof","MissionGroup/Paths/OrcAnimation");
  157.       $AnimationOrc.setMoveSpeed(0.75);
  158.       $AnimationOrc.mountImage(CrossbowImage,0);
  159.       $AnimationOrc.setInventory(CrossbowAmmo,1000);
  160.    }
  161.    $AnimationOrc.pushTask("followPath(\"MissionGroup/Paths/OrcAnimation\",100)");
  162. }
  163.  
  164. function deleteAnimationOrc()
  165. {
  166.    if (isObject($AnimationOrc)) {
  167.       $AnimationOrc.clearTasks();
  168.       $AnimationOrc.pushTask("followPath(\"MissionGroup/Paths/OrcAnimation\",0)");
  169.       $AnimationOrc.pushTask("done()");
  170.    }
  171. }
  172.  
  173. function AnimationOrcPlay(%anim)
  174. {
  175.    $AnimationOrc.clearTasks();
  176.    $AnimationOrc.pushTask("animate(\""@%anim@"\")");
  177. }
  178.  
  179. function newDancingOrc()
  180. {
  181.    if (isObject($DancingOrc))
  182.       $DancingOrc.clearTasks();
  183.    else {
  184.       $DancingOrc = AIPlayer::spawnOnPath("Dance","MissionGroup/Paths/OrcDance");
  185.       $DancingOrc.setMoveSpeed(0.75);
  186.    }
  187.    $DancingOrc.pushTask("followPath(\"MissionGroup/Paths/OrcDance\",100)");
  188.    $DancingOrc.pushTask("animate(\"dance\")");
  189. }
  190.  
  191. function deleteDancingOrc()
  192. {
  193.    if (isObject($DancingOrc)) {
  194.       $DancingOrc.clearTasks();
  195.       $DancingOrc.pushTask("followPath(\"MissionGroup/Paths/OrcDance\",0)");
  196.       $DancingOrc.pushTask("done()");
  197.    }
  198. }
  199.  
  200. function newDetailOrc()
  201. {
  202.    if (isObject($DetailOrc)) {
  203.       $DetailOrc.clearTasks();
  204.       $DetailOrc.unmountImage(0);
  205.       $DetailOrc.unmountImage(1);
  206.       $DetailOrc.setArmThread("looknw");
  207.    }
  208.    else {
  209.       $DetailOrc = AIPlayer::spawnOnPath("Detail","MissionGroup/Paths/OrcDetail");
  210.       $DetailOrc.setMoveSpeed(0.75);
  211.       $DetailOrc.setArmThread("looknw");
  212.    }
  213.    $DetailOrc.pushTask("followPath(\"MissionGroup/Paths/OrcDetail\",100)");
  214. }
  215.  
  216. function deleteDetailOrc()
  217. {
  218.    if (isObject($DetailOrc)) {
  219.       $DetailOrc.clearTasks();
  220.       $DetailOrc.pushTask("followPath(\"MissionGroup/Paths/OrcDetail\",0)");
  221.       $DetailOrc.pushTask("done()");
  222.    }
  223. }
  224.  
  225. function mountDetailOrc(%object,%slot)
  226. {
  227.    if ($DetailOrc.getMountedImage(%slot) == %object.getId()) {
  228.       $DetailOrc.unmountImage(%slot);
  229.       if (%slot == 0)
  230.          $DetailOrc.setArmThread("looknw");
  231.    }
  232.    else {
  233.       $DetailOrc.mountImage(%object,%slot);
  234.       if (%slot == 0)
  235.          $DetailOrc.setArmThread("look");
  236.    }
  237. }
  238.  
  239. function newShootingOrc()
  240. {
  241.    if (isObject($ShootingOrc))
  242.       $ShootingOrc.clearTasks();
  243.    else {
  244.       $ShootingOrc = AIPlayer::spawnOnPath("Detail","MissionGroup/Paths/OrcShooting");
  245.       $ShootingOrc.mountImage(CrossbowImage,0);
  246.       $ShootingOrc.setInventory(CrossbowAmmo,500000);
  247.       $ShootingOrc.setMoveSpeed(0.75);
  248.    }
  249.    $ShootingOrc.pushTask("followPath(\"MissionGroup/Paths/OrcShooting\",100)");
  250.    $ShootingOrc.pushTask("aimAt(\"MissionGroup/Scenes/ProjectileScene/target\")");
  251.    $ShootingOrc.pushTask("wait(1)");   
  252.    $ShootingOrc.pushTask("fire(true)");
  253. }
  254.  
  255. function deleteShootingOrc()
  256. {
  257.    if (isObject($ShootingOrc)) {
  258.       $ShootingOrc.clearTasks();
  259.       $ShootingOrc.pushTask("fire(false)");
  260.       $ShootingOrc.pushTask("followPath(\"MissionGroup/Paths/OrcShooting\",0)");
  261.       $ShootingOrc.pushTask("done()");
  262.    }
  263. }
  264.  
  265.  
  266. //-----------------------------------------------------------------------------
  267. // Environment effects
  268. //-----------------------------------------------------------------------------
  269.  
  270. function startRain()
  271. {
  272.    if (!isObject($Rain))
  273.       $Rain = new Precipitation() {
  274.          datablock = "HeavyRain";
  275.          minSpeed = 10;
  276.          maxSpeed = 15;
  277.          numDrops = 800;
  278.          boxWidth = 80;
  279.          boxHeight = 50;
  280.          minMass = 0.05;
  281.          maxMass = 5;
  282.          rotateWithCamVel = true;
  283.          doCollision = true;
  284.          useTurbulence = true;
  285.          maxTurbulence = 0.1;
  286.          turbulenceSpeed = 0.2;
  287.  
  288.       };
  289. if (!isObject($Rain2))
  290.       $Rain2 = new Precipitation() {
  291.          datablock = "HeavyRain2";
  292.          minSpeed = 0.3;
  293.          maxSpeed = 0.6;
  294.          numDrops = 100;
  295.          boxWidth = 100;
  296.          boxHeight = 100;
  297.          minMass = 0.01;
  298.          maxMass = 1;
  299.          maxTurbulence = 0.05;
  300.          turbulenceSpeed = 0.15;
  301.          rotateWithCamVel = true;
  302.          doCollision = false;
  303.          useTurbulence = true;
  304.       };
  305.  
  306. }
  307.  
  308. function stopRain()
  309. {
  310.    if (isObject($Rain))
  311.       $Rain.delete();
  312.    $Rain = "";
  313.  
  314. if (isObject($Rain2))
  315.       $Rain2.delete();
  316.    $Rain2 = "";
  317.  
  318. }
  319.  
  320. function startLightning()
  321. {
  322.    if (!isObject($Lightning))
  323.       $Lightning = new Lightning() {
  324.          position = "350 300 180";
  325.          scale = "250 400 500";
  326.          dataBlock = "LightningStorm";
  327.          strikesPerMinute = "50";
  328.          strikeWidth = "2.5";
  329.          chanceToHitTarget = "100";
  330.          strikeRadius = "50";
  331.          boltStartRadius = "20";
  332.          color = "1.000000 1.000000 1.000000 1.000000";
  333.          fadeColor = "0.400000 0.400000 0.100000 1.000000";
  334.          useFog = "1";
  335.          locked = "false";
  336.       };
  337. }
  338.  
  339. function stopLightning()
  340. {
  341.    if (isObject($Lightning))
  342.       $Lightning.delete();
  343.    $Lightning = "";
  344. }
  345.  
  346.  
  347. //-----------------------------------------------------------------------------
  348. // Scenes
  349. //-----------------------------------------------------------------------------
  350.  
  351. function InteriorSpaces::open(%this)
  352. {
  353.    Parent::open(%this);
  354.    newAnimationOrc();
  355. }
  356.  
  357. function InteriorSpaces::close(%this)
  358. {
  359.    Parent::close(%this);
  360.    deleteAnimationOrc();
  361. }
  362.  
  363. function OrcAnimationScene::open(%this)
  364. {
  365.    Parent::open(%this);
  366.    newAnimationOrc();
  367.    deleteDancingOrc();
  368.    $AnimationOrc.pushTask("wait(0.5)");
  369.    $AnimationOrc.pushTask("animate(\"celwave\")");
  370. }
  371.  
  372. function OrcDanceScene::open(%this)
  373. {
  374.    Parent::open(%this);
  375.    deleteAnimationOrc();
  376.    newDancingOrc();
  377. }
  378.  
  379. function OrcShadowScene::open(%this)
  380. {
  381.    Parent::open(%this);
  382.    newDancingOrc();
  383.    deleteDetailOrc();
  384. }
  385.  
  386. function OrcDetailScene::open(%this)
  387. {
  388.    Parent::open(%this);
  389.    deleteDancingOrc();
  390.    newDetailOrc();
  391.    %this.detailSetting = $Pref::TS::detailAdjust;
  392.    %this.screenError = $pref::TS::screenError; 
  393.    DetailSceneSlider.setValue($Pref::TS::detailAdjust);
  394. }
  395.  
  396. function OrcDetailScene::close(%this)
  397. {
  398.    $Pref::TS::detailAdjust = %this.detailSetting;
  399.    $Pref::TS::detailAdjust = %this.screenError;
  400.    DetailSceneSlider.setValue(%this.detailSetting);
  401.    Parent::close(%this);
  402. }
  403.  
  404. function OrcDetailScene::slider(%this)
  405. {
  406.    $Pref::TS::detailAdjust = DetailSceneSlider.getValue();
  407.    $Pref::TS::screenError = 1 + 35 * (1 - $Pref::TS::detailAdjust); 
  408. }
  409.  
  410. function OrcMountingScene::open(%this)
  411. {
  412.    Parent::open(%this);
  413.    newDetailOrc();
  414.    deleteShootingOrc();
  415. }
  416.  
  417. function ProjectileScene::open(%this)
  418. {
  419.    Parent::open(%this);
  420.    deleteDetailOrc();
  421.    newShootingOrc();
  422. }
  423.  
  424. function ExplosionScene::open(%this)
  425. {
  426.    Parent::open(%this);
  427.    newShootingOrc();
  428. }
  429.  
  430. function FXLightScene::open(%this)
  431. {
  432.    Parent::open(%this);
  433.    deleteShootingOrc();
  434. }
  435.  
  436. function MirrorScene::open(%this)
  437. {
  438.    Parent::open(%this);
  439.    stopRain();
  440. }
  441.  
  442. function PrecipitationScene::open(%this)
  443. {
  444.    Parent::open(%this);
  445.    startRain();
  446.    stopLightning();
  447. }
  448.  
  449. function LightningScene::open(%this)
  450. {
  451.    Parent::open(%this);
  452.    startRain();
  453.    startLightning();
  454.    Sky.stormFog(0,3);
  455. }
  456.  
  457. function FogScene::open(%this)
  458. {
  459.    Parent::open(%this);
  460.    Sky.stormFog(1,3);
  461.    startRain();
  462.    stopLightning();
  463. }
  464.  
  465. function InFogScene::open(%this)
  466. {
  467.    Parent::open(%this);
  468.    Sky.stormFog(1,3);
  469.    startRain();
  470. }
  471.  
  472. function WaterScene::open(%this)
  473. {
  474.    Parent::open(%this);
  475.    Sky.stormFog(0,3);
  476.    stopRain();
  477. }
  478.  
  479. function TorqueScene::open(%this)
  480. {
  481.    Parent::open(%this);
  482.    TorqueLogoScreen.visible = 0;
  483. }
  484.  
  485. //-----------------------------------------------------------------------------
  486. // Path Camera
  487. //-----------------------------------------------------------------------------
  488.  
  489. datablock PathCameraData(LoopingCam)
  490. {
  491.    mode = "";
  492. };
  493.  
  494. function LoopingCam::onNode(%this,%camera,%node)
  495. {
  496.    if (%node == %camera.loopNode) {
  497.       %camera.pushPath(%camera.path);
  498.       %camera.loopNode += %camera.path.getCount();
  499.    }
  500. }
  501.  
  502. function PathCamera::followPath(%this,%path)
  503. {
  504.    %this.path = %path;
  505.    if (!(%this.speed = %path.speed))
  506.       %this.speed = 100;
  507.    if (%path.isLooping)
  508.       %this.loopNode = %path.getCount() - 2;
  509.    else
  510.       %this.loopNode = -1;
  511.    
  512.    %this.pushPath(%path);   
  513.    %this.popFront();
  514. }
  515.  
  516. function PathCamera::pushPath(%this,%path)
  517. {
  518.    for (%i = 0; %i < %path.getCount(); %i++)
  519.       %this.pushNode(%path.getObject(%i));
  520. }
  521.  
  522. function PathCamera::pushNode(%this,%node)
  523. {
  524.    if (!(%speed = %node.speed))
  525.       %speed = %this.speed;
  526.    if ((%type = %node.type) $= "")
  527.       %type = "Normal";
  528.    if ((%smoothing = %node.smoothing) $= "")
  529.       %smoothing = "Linear";
  530.    %this.pushBack(%node.getTransform(),%speed,%type,%smoothing);
  531. }
  532.  
  533.  
  534. //-----------------------------------------------------------------------------
  535.  
  536. datablock ItemData(Logo)
  537. {
  538.    // An item is used
  539.    category = "Misc";
  540.    shapeFile = "~/data/shapes/logo/torque_logo.dts";
  541. };
  542.